﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QASampling.BusinessObject;
using SMcMaster;
using System.Threading;

namespace QASampling.UI
{
    public partial class BaseFormSPTWithHeader : BaseForm
    {
        private string docid;
        public string DocId
        {
            get { return docid; }
            set { docid = value; }
        }

        private string namaPTKP;
        public string NamaPTKP
        {
            get { return namaPTKP; }
            set { namaPTKP = value; }
        }

        private string alamat;
        public string Alamat
        {
            get { return alamat; }
            set { alamat = value; }
        }

        private string npwp;
        public string NPWP
        {
            get { return npwp; }
            set { npwp = value; }
        }

        private string masapajak;
        public string MasaPajak
        {
            get { return masapajak; }
            set { masapajak = value; }
        }

        internal TabOrderManager tom;

        public BaseFormSPTWithHeader()
        {
            InitializeComponent();
        }

        private void btnSuspend_Click(object sender, EventArgs e)
        {
            try
            {
                InputMessageBox im = new InputMessageBox();
                im.Text = StatusSPT.Suspend.ToString();
                if (im.Show("Masukkan alasan kenapa SPT ini ingin di SUSPEND") == DialogResult.Cancel)
                    return;

                QAController.UpdateQAJob("SPT di-SUSPEND dengan pesan: " + im.Input, StatusSPT.Suspend.ToString().ToLower());
                Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnHold_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = "Anda yakin akan menghentikan tugas ini sementara?";
                if (MessageBox.Show(msg, StatusSPT.Hold.ToString(), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    return;

                QAController.UpdateQAJob("SPT di-hold", StatusSPT.Hold.ToString().ToLower());
                Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BaseFormSPTWithHeader_Load(object sender, EventArgs e)
        {
            using (frmSplash splash = new frmSplash())
            {
                try
                {
                    if (!DesignMode)
                    {
                        splash.ShowForm();

                        tom = new TabOrderManager(this);
                        tom.SetTabOrder(TabOrderManager.TabScheme.AcrossFirst);

                        if (QAController.FieldErrorMsg != null)
                            QAController.FieldErrorMsg = null;
                        QAController.FieldErrorMsg = new List<QAFieldErrorMsg>();

                        try
                        {
                            if (!DesignMode)
                            {
                                QAJob job = QAController.Job;
                                if (job != null)
                                {
                                    int ID = QAController.GetDocIDFromStellent(job.Barcode, job.Npwp, job.NamaPTKP);
                                    System.IO.Stream stream = null;
                                    MIIWebservice ws = new MIIWebservice();
                                    if (ID != -1)
                                        stream = ws.GetFile(ID);
                                    if (stream != null)
                                        qapdfViewer1.ShowStream(stream);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            MessageBox.Show(this, "Tidak bisa membuka image", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                        if (QAController.TipeSPT == TipeSPT.Balance)
                            btnUnbalance.Visible = true;

                        foreach (TabPage tp in tabControl1.TabPages)
                        {
                            this.tabControl1.SelectedTab = tp;
                            tp.AutoScroll = true;
                        }
                        if (tabControl1.HasChildren)
                            this.tabControl1.SelectTab(0);

                        this.txtNamaPKP.Focus();
                        this.ValidateChildren();
                        this.WindowState = FormWindowState.Maximized;
                        this.txtNamaPKP.Focus();
                        this.BringToFront();
                        this.Update();

                        if (splash != null)
                        {
                            splash.CloseForm();
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (splash != null)
                    {
                        splash.CloseForm();
                    }
                }                
            }
        }
    
        private void btnPass_Click(object sender, EventArgs e)
        {
            try
            {
                this.Visible = false;
                QAController.FieldErrorMsg.Clear();
                this.ValidateChildren();
                this.Visible = true;
                using (frmFieldWithError f = new frmFieldWithError())
                {
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        string msg = "Anda yakin tugas ini sudah dikoreksi dengan benar?";
                        if (MessageBox.Show(msg, StatusSPT.Pass.ToString(), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                            return;

                        QAController.UpdateQAJob("SPT dinyatakan PASS", StatusSPT.Pass.ToString().ToLower());
                        Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }            
        }

        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }

        private void btnUnbalance_Click(object sender, EventArgs e)
        {
            try
            {
                string msg = "Anda yakin ingin meng-UNBALANCE kan SPT ini?";
                if (MessageBox.Show(msg, StatusSPT.Pass.ToString(), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                    return;

                QAController.UpdateQAJob("SPT dinyatakan UNBALANCE", QASampling.BusinessObject.StatusSPT.Unbalance.ToString().ToLower());
                Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnHideOrShow_Click(object sender, EventArgs e)
        {
            scImage.Panel1Collapsed = !scImage.Panel1Collapsed;
        }

        private void BaseFormSPTWithHeader_FormClosed(object sender, FormClosedEventArgs e)
        {
           // globalEventProvider1.KeyDown -=new KeyEventHandler(globalEventProvider1_KeyDown);
        }

        protected override bool ProcessCmdKey(ref Message message, Keys keys)
        {
            switch (keys)
            {
                case Keys.W | Keys.Control:
                    //Process action here.
                    qapdfViewer1.NextPage();
                    break;
                case Keys.Q | Keys.Control:
                    qapdfViewer1.PrevPage();
                    break;
                //--added by Vincent 20130110 for shortcut btn pass hold suspend
                case Keys.P | Keys.Control:
                    btnPass_Click(new object(), new EventArgs());  
                    break;
                case Keys.S | Keys.Control:
                    btnSuspend_Click(new object(), new EventArgs());
                    break;
                case Keys.H | Keys.Control:
                    btnHold_Click(new object(), new EventArgs());
                    break;
                case Keys.F1:
                    btnHideOrShow_Click(new object(), new EventArgs());
                    break;                
                //--------------------------------------------------------------
                   
                case Keys.Up | Keys.Control:
                    qapdfViewer1.ScrollVerticalUp();
                    break;
                case Keys.Down | Keys.Control:
                    qapdfViewer1.ScrollVerticalDown();
                    break;
                case Keys.Right | Keys.Control:
                    qapdfViewer1.ScrollHorizontalRight();
                    break;
                case Keys.Left | Keys.Control:
                    qapdfViewer1.ScrollHorizontalLeft();
                    break;
                case Keys.Add | Keys.Control:
                    qapdfViewer1.ZoomIn();
                    break;
                case Keys.Subtract | Keys.Control:
                    qapdfViewer1.ZoomOut();
                    break;        
                case Keys.Oemplus | Keys.Control:
                    qapdfViewer1.ZoomIn();
                    break;
                case Keys.OemMinus | Keys.Control:
                    qapdfViewer1.ZoomOut();
                    break;
            }

            return false;
        }
    }
}
